/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.marks.editor;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import mtt.marks.editor.events.ShowMarkEditorViewEvent;
import mtt.marks.events.ShowMarksViewEvent;
import mtt.rms.dataObjects.Mark;
import mtt.rms.recordstores.marks.events.StoreMarkEvent;
import mtt.rms.recordstores.marks.events.UpdateMarkEvent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.ShowDisplayAbleEvent;
import hmvc.j2me.events.TerminateEvent;

/**
 * 
 * 
 * @author jens.meiss
 */
public class MarkEditorController extends AController implements CommandListener {
	
	private MarkEditorModel model;
	
	private MarkEditorView view;
	
	public MarkEditorController() {
		
		setModel(new MarkEditorModel());
		getModel().setMark(new Mark());
		
		setView(new MarkEditorView());
		
	}
	
	public MarkEditorController(Mark mark) {
		
		setModel(new MarkEditorModel());
		getModel().setMark(mark);
		getModel().setNewMark(false);
		
		setView(new MarkEditorView());
	}

	public void commandAction(Command command, Displayable d) {

		if (command == getView().getCommandStore()) {

			/*
			 * update an existing or store a new mark
			 */
			if (getModel().isNewMark()) {
				
				storeMark();
				
			} else {
				updateMark();
			}
		}

		fireEventUp(new ShowMarksViewEvent());
		fireEvent(new TerminateEvent(), false, true, false);
		

	}

	private MarkEditorModel getModel() {
		return model;
	}

	private MarkEditorView getView() {
		return view;
	}

	public void init() {

		register(ShowMarkEditorViewEvent.class);
		
		TextField textFieldName = new TextField("Name des Fachs: ", getModel().getMark().getName(), 30, TextField.ANY);
		getView().setTextFieldName(textFieldName);
		
		TextField textFieldMark = new TextField("Note/Punkte", getModel().getMark().getMark(), 3, TextField.DECIMAL);
		getView().setTextFieldMark(textFieldMark);		
		
		Command commandBack = new Command("Zurück", Command.BACK, 0);
		getView().setCommandBack(commandBack);
		
		Command commandStore = new Command("Speichern", Command.OK, 1);
		getView().setCommandStore(commandStore);
		
		Form form = null;
		if (getModel().isNewMark()) {
			form = new Form("Neue Note/Punkte speichern");
		} else {
			form = new Form("Note/Punkte bearbeiten");
		}
		form.setCommandListener(this);
		getView().setForm(form);
		
		form.append(textFieldName);
		form.append(textFieldMark);
		
		form.addCommand(commandBack);
		form.addCommand(commandStore);

	}

	public void receiveEvent(IEvent event) {
		
		if (event instanceof ShowMarkEditorViewEvent) {
			fireEventUp(new ShowDisplayAbleEvent(getView().getDisplayable()));
		}
		
	}

	private void setModel(MarkEditorModel model) {
		this.model = model;
	}

	private void setView(MarkEditorView view) {
		this.view = view;
	}

	private void storeMark() {
		
		Mark mark = getModel().getMark();
		mark.setMark(getView().getTextFieldMark().getString());
		mark.setName(getView().getTextFieldName().getString());
		
		fireEventUp(new StoreMarkEvent(mark));
	}

	public void terminate() {
	}

	private void updateMark() {
		
		Mark mark = getModel().getMark();

		mark.setName(getView().getTextFieldName().getString());
		mark.setMark(getView().getTextFieldMark().getString());
		
		fireEventUp(new UpdateMarkEvent(mark));
		fireEventUp(new ShowMarksViewEvent());
	}

}
